package file;

import org.junit.jupiter.api.Test;

import java.io.*;
import java.util.concurrent.TimeUnit;

public class FileCopy {

    @Test
    public void fileCopyBySingleByte() throws IOException {

        //把文件从一个位置拷贝到另一个位置
        /*
        用输入流读取文件
        在用输出流写入文件
         */

        //目标文件路径，复制的文件路径

        String filePath = "D:\\桌面\\ddl\\test.jpg";
        File file = new File(filePath);
        String desFilePath = "D:\\aaaa\\testCopy.jpg";

        //读取输出
        FileInputStream inputStream = new FileInputStream(file);
        FileOutputStream outputStream = new FileOutputStream(desFilePath,true);
        int readByte = 0;
        while((readByte = inputStream.read())!=-1){
            outputStream.write(readByte);
        }
        //关闭输入输出流
        inputStream.close();
        outputStream.close();
    }

    @Test
    public void fileCopyByMultiByte() throws IOException {

        //把文件从一个位置拷贝到另一个位置
        /*
        用输入流读取文件
        在用输出流写入文件
         */

        //目标文件路径，复制的文件路径

        String filePath = "D:\\桌面\\ddl\\test.jpg";
        File file = new File(filePath);
        String desFilePath = "D:\\aaaa\\testCopy1.jpg";

        //读取输出
        FileInputStream inputStream = new FileInputStream(file);
        FileOutputStream outputStream = new FileOutputStream(desFilePath,true);
        int readLength = 0;
        byte[] b = new byte[1024];
        while((readLength = inputStream.read(b))!=-1){
            outputStream.write(b,0,readLength);
        }
        //关闭输入输出流
        inputStream.close();
        outputStream.close();
    }

}
